<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" width="500px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="所属分类" prop="cid">
        <el-input v-model="parentNode.name" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="所属分组" prop="groupId">
        <el-select v-model="dataForm.groupId" placeholder="请选择" filterable @change="selectChange">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="参数名" prop="name">
        <el-input v-model="dataForm.name" placeholder="参数名"></el-input>
      </el-form-item>
      <el-form-item label="通用属性" prop="generic">
        <el-switch
          v-model="dataForm.generic"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="搜索过滤" prop="searching">
        <el-switch
          v-model="dataForm.searching"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="数字类型" prop="whetherNumeric">
        <el-switch
          v-model="dataForm.whetherNumeric"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="单位" prop="unit" v-if="dataForm.whetherNumeric == 1">
        <el-input v-model="dataForm.unit" placeholder="数字类型参数的单位，非数字类型可以为空"></el-input>
      </el-form-item>
      <el-form-item label="分段间隔" prop="segments" v-if="dataForm.whetherNumeric == 1">
        <el-input v-model="dataForm.segments" placeholder="如CPU频率间隔：0.5-1.0"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      parentNode: {
        id: {
          type: Number,
          default: 0
        },
        name: {
          type: String
        }
      }
    },
    data() {
      return {
        options: [],
        visible: false,
        dataForm: {
          id: 0,
          cid: '',
          groupId: '',
          name: '',
          whetherNumeric: 0,
          unit: '',
          generic: '',
          searching: '',
          segments: '',
          categoryName: '',
          groupName: ''
        },
        dataRule: {
          groupId: [
            {required: true, message: '所属分组不能为空', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '参数名不能为空', trigger: 'blur'}
          ],
          unit: [
            {validator: (rule, value, collback) => {
                if (this.dataForm.whetherNumeric == 1 && value == '') {
                  collback(new Error("启用数字类型，必须填写单位"))
                }else{
                  collback()
                }
              }, trigger: 'blur'}
          ],
          segments: [
            {
              validator: (rule, value, collback) => {
                if (this.dataForm.whetherNumeric == 1 && value == '') {
                  collback(new Error("启用数字类型，必须设置分段间隔值"))
                }else{
                  collback()
                }
              }, trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      selectChange(value){
        let option = this.options.filter(item=>{return item.id == value})
        this.dataForm.groupName = option[0].name;
      },
      getOptions() {
        let cid = this.parentNode.id;
        this.$http({
          url: this.$http.adornUrl(`/item/specgroup/list/${cid}`),
          method: 'get',
        }).then(({data}) => {
          this.options = data.page.list;
        })
      },
      init(id) {
        this.dataForm.cid = this.parentNode.id
        this.dataForm.categoryName = this.parentNode.name;
        this.getOptions()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/item/specparam/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cid = data.specParam.cid
                this.dataForm.groupId = data.specParam.groupId
                this.dataForm.name = data.specParam.name
                this.dataForm.whetherNumeric = data.specParam.whetherNumeric
                this.dataForm.unit = data.specParam.unit
                this.dataForm.generic = data.specParam.generic
                this.dataForm.searching = data.specParam.searching
                this.dataForm.segments = data.specParam.segments
                this.dataForm.categoryName = data.specParam.categoryName
                this.dataForm.groupName = data.specParam.groupName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          console.log(valid)
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/item/specparam/${!this.dataForm.id ? 'save' : 'update'}`),
              method: this.dataForm.id != 0 ? 'put' : 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'cid': this.dataForm.cid,
                'groupId': this.dataForm.groupId,
                'name': this.dataForm.name,
                'whetherNumeric': this.dataForm.whetherNumeric,
                'unit': this.dataForm.unit,
                'generic': this.dataForm.generic,
                'searching': this.dataForm.searching,
                'segments': this.dataForm.segments,
                'categoryName':this.dataForm.categoryName,
                'groupName':this.dataForm.groupName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '修改成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
